package backup;

import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.List;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;

import view.DeviceRegister;

import components.Device;

import device.DeviceHelper;

public class TesteRegister extends Panel {

    private static final long serialVersionUID = 1L;

    private Label label = null;

    private TextField textField = null;

    private Label label1 = null;

    private TextField textField1 = null;

    private Label label2 = null;

    private List list = null;

    private Label label3 = null;

    private Label Porta = null;

    private TextField textField2 = null;

    private Panel panel = null;

    private Panel panel1 = null;

    private Button button = null;

    private Button button1 = null;

    private Label label4 = null;

    private final DeviceRegister deviceRegister;

    /**
     * This is the default constructor
     * @param deviceRegister 
     */
    public TesteRegister(DeviceRegister deviceRegister) {
        super();
        this.deviceRegister = deviceRegister;
        setSize(500, 500);
        setBackground(Color.WHITE);
        setLocation(135, 85);
        initialize();
    }

    /**
     * This method initializes this
     * 
     * @return void
     */
    private void initialize() {
        GridBagConstraints gridBagConstraints111 = new GridBagConstraints();
        gridBagConstraints111.gridx = 3;
        gridBagConstraints111.gridy = 2;
        label4 = new Label();
        GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
        gridBagConstraints9.gridx = 2;
        gridBagConstraints9.gridy = 4;
        GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
        gridBagConstraints21.fill = GridBagConstraints.HORIZONTAL;
        gridBagConstraints21.gridy = 3;
        gridBagConstraints21.weightx = 1.0;
        gridBagConstraints21.anchor = GridBagConstraints.WEST;
        gridBagConstraints21.gridx = 2;
        GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
        gridBagConstraints11.gridx = 0;
        gridBagConstraints11.gridy = 3;
        Porta = new Label();
        Porta.setText("Porta");
        Porta.setAlignment(Label.RIGHT);
        GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
        gridBagConstraints6.gridx = 2;
        gridBagConstraints6.gridy = 6;
        label3 = new Label();
        label3.setText("Equipamentos");
        GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
        gridBagConstraints5.fill = GridBagConstraints.CENTER;
        gridBagConstraints5.gridy = 7;
        gridBagConstraints5.weightx = 1.0;
        gridBagConstraints5.weighty = 1.0;
        gridBagConstraints5.gridheight = 1;
        gridBagConstraints5.anchor = GridBagConstraints.NORTH;
        gridBagConstraints5.gridx = 2;
        GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
        gridBagConstraints4.gridx = 2;
        gridBagConstraints4.gridy = 0;
        label2 = new Label();
        label2.setText("Cadastro de Equipamentos");
        label2.setFont(new Font("Arial", Font.BOLD, 15));
        GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
        gridBagConstraints3.fill = GridBagConstraints.BOTH;
        gridBagConstraints3.gridy = 2;
        gridBagConstraints3.weightx = 1.0;
        gridBagConstraints3.anchor = GridBagConstraints.WEST;
        gridBagConstraints3.gridx = 2;
        GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
        gridBagConstraints2.gridx = 0;
        gridBagConstraints2.anchor = GridBagConstraints.EAST;
        gridBagConstraints2.gridy = 2;
        label1 = new Label();
        label1.setText("Host");
        label1.setAlignment(Label.RIGHT);
        GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
        gridBagConstraints1.fill = GridBagConstraints.BOTH;
        gridBagConstraints1.gridy = 1;
        gridBagConstraints1.anchor = GridBagConstraints.WEST;
        gridBagConstraints1.weightx = 1.0;
        gridBagConstraints1.gridwidth = 1;
        gridBagConstraints1.gridx = 2;
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.anchor = GridBagConstraints.EAST;
        gridBagConstraints.gridy = 1;
        label = new Label();
        label.setText("Nome");
        label.setAlignment(Label.RIGHT);
        this.setSize(300, 200);
        this.setLayout(new GridBagLayout());
        //this.setBackground(new DVBColor(0, 255, 0, 100));
        this.add(label, gridBagConstraints);
        this.add(getTextField(), gridBagConstraints1);
        this.add(label1, gridBagConstraints2);
        this.add(getTextField1(), gridBagConstraints3);
        this.add(label2, gridBagConstraints4);
        this.add(getList(), gridBagConstraints5);
        this.add(label3, gridBagConstraints6);
        this.add(Porta, gridBagConstraints11);
        this.add(getTextField2(), gridBagConstraints21);
        this.add(getPanel1(), gridBagConstraints9);
        this.add(label4, gridBagConstraints111);
    }

    /**
     * This method initializes textField	
     * 	
     * @return java.awt.TextField	
     */
    private TextField getTextField() {
        if (textField == null) {
            textField = new TextField();
            textField.setText("MicroOndas");
            textField.addTextListener(new TextListener() {

                public void textValueChanged(TextEvent e) {
                    System.out.println("oii");
                }
            });
            textField.addKeyListener(new KeyListener() {

                public void keyTyped(KeyEvent e) {
                    System.out.println("oi keyTyped: " + e.getKeyChar());
                }

                public void keyReleased(KeyEvent e) {
                    System.out.println("oi keyReleased:" + e.getKeyChar() + " keyCode: " + e.getKeyCode());
                }

                public void keyPressed(KeyEvent e) {
                    System.out.println("oi keyPressed: " + e.getKeyChar() + " keyCode: " + e.getKeyCode());
                    String text = textField.getText();
                    System.out.println(textField.getSelectionStart() + " " + textField.getSelectionEnd());
                    System.out.println(textField.getSelectedText());
                    switch (e.getKeyCode()) {
                        case KeyEvent.VK_BACK_SPACE:
                            System.out.println("back");
                            if (text.length() == 0) {
                                return;
                            }
                            if (textField.getSelectionEnd() > 0) {
                                String sub = text.substring(0, textField.getSelectionStart());
                                sub += text.substring(textField.getSelectionEnd(), text.length());
                                textField.setText(sub);
                            } else {
                                textField.setText(text.substring(0, text.length() - 1));
                            }
                            break;
                        default:
                            textField.setText(text + e.getKeyChar());
                            break;
                    }
                }
            });
        }
        return textField;
    }

    /**
     * This method initializes textField1	
     * 	
     * @return java.awt.TextField	
     */
    private TextField getTextField1() {
        if (textField1 == null) {
            textField1 = new TextField();
            textField1.setText("127.0.0.1");
        }
        return textField1;
    }

    /**
     * This method initializes list	
     * 	
     * @return java.awt.List	
     */
    private List getList() {
        if (list == null) {
            list = new List();
            loadList();
        }
        return list;
    }

    private void loadList() {
        list.removeAll();
        java.util.List<String> devicesName = DeviceHelper.getInstance().getDevicesNames();
        for (String name : devicesName) {
            list.add(name);
        }
    }

    /**
     * This method initializes textField2	
     * 	
     * @return java.awt.TextField	
     */
    private TextField getTextField2() {
        if (textField2 == null) {
            textField2 = new TextField();
            textField2.setText("80");
        }
        return textField2;
    }

    /**
     * This method initializes panel	
     * 	
     * @return java.awt.Panel	
     */
    private Panel getPanel() {
        if (panel == null) {
            panel = new Panel();
            panel.setLayout(new GridBagLayout());
        }
        return panel;
    }

    /**
     * This method initializes panel1	
     * 	
     * @return java.awt.Panel	
     */
    private Panel getPanel1() {
        if (panel1 == null) {
            GridLayout gridLayout = new GridLayout();
            gridLayout.setRows(1);
            gridLayout.setColumns(2);
            panel1 = new Panel();
            panel1.setLayout(gridLayout);
            panel1.add(getButton(), null);
            panel1.add(getButton1(), null);
        }
        return panel1;
    }

    /**
     * This method initializes button	
     * 	
     * @return java.awt.Button	
     */
    private Button getButton() {
        if (button == null) {
            button = new Button();
            button.setLabel("Gravar");
            button.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    Device device = new Device();
                    device.setName(textField.getText());
                    device.setHost(textField1.getText());
                    device.setPort(new Integer(textField2.getText()));
                    DeviceHelper.getInstance().saveDevice(device);
                    loadList();
                    deviceRegister.requestFocus();
                }
            });
        }
        return button;
    }

    /**
     * This method initializes button1	
     * 	
     * @return java.awt.Button	
     */
    private Button getButton1() {
        if (button1 == null) {
            button1 = new Button();
            button1.setLabel("Voltar");
            button1.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    deviceRegister.voltar();
                }
            });
        }
        return button1;
    }
} //  @jve:decl-index=0:visual-constraint="46,-134"
